//
//  MbRewardAd.m
//  MbAdsSdk
//
//  Created by 吴奶强 on 2021/12/17.
//

#import "MbRewardAd.h"
#import <BUAdSDK/BUAdSDK.h>
#import <KSAdSDK/KSAdSDK.h>
#import <ABUADSDK/ABUADSDK.h>
#import "GDTRewardVideoAd.h"


@interface MbRewardAd()<ABURewardedVideoAdDelegate,BUNativeExpressRewardedVideoAdDelegate,GDTRewardedVideoAdDelegate,KSRewardedVideoAdDelegate>

@property(nonatomic,strong)ABURewardedVideoAd *gromoreVideoAd;

@property(nonatomic,strong)BUNativeExpressRewardedVideoAd *ttVideoAd;

@property(nonatomic,strong)GDTRewardVideoAd *gdtVideoAd;

@property(nonatomic,strong)KSRewardedVideoAd *ksVideoAd;

@property(nonatomic,assign)NSInteger index;

@property(nonatomic,strong)NSError *error;

@end

@implementation MbRewardAd


-(void)loadReward{
    if(!self.adItems || self.adItems.count == self.index ){
        if(!self.error){
            self.error = [[NSError alloc]initWithDomain:@"配置信息错误" code:0 userInfo:nil];
        }
        if ([self.delegate respondsToSelector:@selector(rewardAdDidError:)]) {
            [self.delegate rewardAdDidError:self.error];
        }
        return;
    }
    AdItem *adItem = [self.adItems objectAtIndex:self.index];
    if(adItem.plat == Pangolin){
        BURewardedVideoModel *model = [[BURewardedVideoModel alloc] init];
        model.userId = self.userId.length == 0 ? @"tag123":self.userId;
        if(adItem.extra.length > 0){
            model.extra = adItem.extra;
        }
        self.ttVideoAd = [[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:adItem.codeNo rewardedVideoModel:model];
        self.ttVideoAd.delegate = self;
        // optional
        self.ttVideoAd.rewardPlayAgainInteractionDelegate = self;
        [self.ttVideoAd loadAdData];
        
    } else if(adItem.plat == GDT){
        
        self.gdtVideoAd = [[GDTRewardVideoAd alloc] initWithPlacementId:adItem.codeNo];
        self.gdtVideoAd.delegate = self;
        self.gdtVideoAd.videoMuted = NO; // 设置激励视频是否静音
        if(adItem.extra.length > 0){
            GDTServerSideVerificationOptions *options = [[GDTServerSideVerificationOptions alloc]init];
            options.userIdentifier = self.userId;
            options.customRewardString = adItem.extra;
            [self.gdtVideoAd setServerSideVerificationOptions:options];
        }
        [self.gdtVideoAd loadAd];
        
    } else if(adItem.plat == KS){
        KSRewardedVideoModel *model = [KSRewardedVideoModel new];
        if(adItem.extra.length > 0){
            model.userId = self.userId;
            model.extra = adItem.extra;
        }
        self.ksVideoAd = [[KSRewardedVideoAd alloc] initWithPosId:adItem.codeNo rewardedVideoModel:model];
        self.ksVideoAd.delegate = self;
        [self.ksVideoAd loadAdData];
    } else if(adItem.plat == GroMore){
        
        
        self.gromoreVideoAd = [[ABURewardedVideoAd alloc] initWithAdUnitID:adItem.codeNo];
        if(adItem.extra.length > 0){
         
            NSString *pangStr = [adItem.extra stringByReplacingOccurrencesOfString:@"gromore" withString:@"csj"];
            NSString *gdtStr = [adItem.extra stringByReplacingOccurrencesOfString:@"gromore" withString:@"ylh"];
            NSString *ksStr = [adItem.extra stringByReplacingOccurrencesOfString:@"gromore" withString:@"ks"];
            [self.gromoreVideoAd addParam:self.userId withKey:@"userId"];
            [self.gromoreVideoAd addParam:pangStr withKey:@"BUAdSDK"];
            [self.gromoreVideoAd addParam:gdtStr withKey:@"GDT"];
            [self.gromoreVideoAd addParam:ksStr withKey:@"Ks"];
        }
       
        self.gromoreVideoAd.delegate = self;
    
        //self.gromoreVideoAd.mutedIfCan = YES;//静音
        __weak typeof(self) weakself = self;
        if([ABUAdSDKManager configDidLoad]){
            [self.gromoreVideoAd loadAdData];
        }else{
            //当前配置未拉取成功，在成功之后会调用该callback
            [ABUAdSDKManager addConfigLoadSuccessObserver:self withAction:^(id  _Nonnull observer) {
                [weakself.gromoreVideoAd loadAdData];
            }];
        }
    }
    self.index ++;
}

- (void)showReward:(UIViewController *)rootVC{
    if (self.gromoreVideoAd && self.gromoreVideoAd.isReady){
        [self.gromoreVideoAd showAdFromRootViewController:rootVC];
    }
    if (self.ttVideoAd){
        [self.ttVideoAd showAdFromRootViewController:rootVC];
    }
    if (self.gdtVideoAd){
        [self.gdtVideoAd showAdFromRootViewController:rootVC];
    }
    
    if (self.ksVideoAd){
        [self.ksVideoAd showAdFromRootViewController:rootVC];
    }
    
}
#pragma mark 穿山甲回调
- (void)nativeExpressRewardedVideoAdDidLoad:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    if ([self.delegate respondsToSelector:@selector(rewardAdDidLoad)]) {
        [self.delegate rewardAdDidLoad];
    }
}

- (void)nativeExpressRewardedVideoAdDidDownLoadVideo:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    if ([self.delegate respondsToSelector:@selector(rewardAdDidDownLoadVideo)]) {
        [self.delegate rewardAdDidDownLoadVideo];
    }
}


- (void)nativeExpressRewardedVideoAdDidVisible:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    if ([self.delegate respondsToSelector:@selector(rewardAdDidVisible)]) {
        [self.delegate rewardAdDidVisible];
    }
}

- (void)nativeExpressRewardedVideoAdServerRewardDidSucceed:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd verify:(BOOL)verify{
    if ([self.delegate respondsToSelector:@selector(rewardAdDidSucceed)]) {
        [self.delegate rewardAdDidSucceed];
    }
}

- (void)nativeExpressRewardedVideoAdDidClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    if ([self.delegate respondsToSelector:@selector(rewardAdDidClose)]) {
        [self.delegate rewardAdDidClose];
    }
}

- (void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error{
    self.error = error;
    [self loadReward];
}

#pragma mark 优量汇回调
- (void)gdt_rewardVideoAdDidLoad:(GDTRewardVideoAd *)rewardedVideoAd{
    if ([self.delegate respondsToSelector:@selector(rewardAdDidLoad)]) {
        [self.delegate rewardAdDidLoad];
    }
}
- (void)gdt_rewardVideoAdVideoDidLoad:(GDTRewardVideoAd *)rewardedVideoAd{
    if ([self.delegate respondsToSelector:@selector(rewardAdDidDownLoadVideo)]) {
        [self.delegate rewardAdDidDownLoadVideo];
    }
}

- (void)gdt_rewardVideoAdWillVisible:(GDTRewardVideoAd *)rewardedVideoAd{
    if ([self.delegate respondsToSelector:@selector(rewardAdDidVisible)]) {
        [self.delegate rewardAdDidVisible];
    }
}

- (void)gdt_rewardVideoAdDidRewardEffective:(GDTRewardVideoAd *)rewardedVideoAd info:(NSDictionary *)info{
    if ([self.delegate respondsToSelector:@selector(rewardAdDidSucceed)]) {
        [self.delegate rewardAdDidSucceed];
    }
}

- (void)gdt_rewardVideoAdDidClose:(GDTRewardVideoAd *)rewardedVideoAd{
    if ([self.delegate respondsToSelector:@selector(rewardAdDidClose)]) {
        [self.delegate rewardAdDidClose];
    }
}

- (void)gdt_rewardVideoAd:(GDTRewardVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error{
    self.error = error;
    [self loadReward];
}

#pragma mark 快手回调

- (void)rewardedVideoAdVideoDidLoad:(KSRewardedVideoAd *)rewardedVideoAd{
    if ([self.delegate respondsToSelector:@selector(rewardAdDidDownLoadVideo)]) {
        [self.delegate rewardAdDidDownLoadVideo];
    }
}

- (void)rewardedVideoAdDidClose:(KSRewardedVideoAd *)rewardedVideoAd{
    if ([self.delegate respondsToSelector:@selector(rewardAdDidClose)]) {
        [self.delegate rewardAdDidClose];
    }
}

#pragma mark gromore回调
//广告加载成功
- (void)rewardedVideoAdDidLoad:(NSObject *)rewardedVideoAd{
    //ABURewardedVideoAd，KSRewardedVideoAd
    if ([self.delegate respondsToSelector:@selector(rewardAdDidLoad)]) {
        [self.delegate rewardAdDidLoad];
    }
    
}
//广告缓存(视频)成功
- (void)rewardedVideoAdDidDownLoadVideo:(ABURewardedVideoAd *)rewardedVideoAd{
    
    if ([self.delegate respondsToSelector:@selector(rewardAdDidDownLoadVideo)]) {
        [self.delegate rewardAdDidDownLoadVideo];
    }
}

//广告展示成功
- (void)rewardedVideoAdDidVisible:(NSObject *)rewardedVideoAd{
    //ABURewardedVideoAd,KSRewardedVideoAd
    if ([self.delegate respondsToSelector:@selector(rewardAdDidVisible)]) {
        [self.delegate rewardAdDidVisible];
    }
}


/// 奖励发放的标识(包括adn的C2C/S2S)
/// @param rewardedVideoAd <#rewardedVideoAd description#>
/// @param rewardInfo <#rewardInfo description#>
/// @param verify <#verify description#>
- (void)rewardedVideoAdServerRewardDidSucceed:(ABURewardedVideoAd *)rewardedVideoAd rewardInfo:(ABUAdapterRewardAdInfo *)rewardInfo verify:(BOOL)verify{
    if ([self.delegate respondsToSelector:@selector(rewardAdDidSucceed)]) {
        [self.delegate rewardAdDidSucceed];
    }
}

//广告加载失败
- (void)rewardedVideoAd:(NSObject *)rewardedVideoAd didFailWithError:(NSError *)error{
    //ABURewardedVideoAd,KSRewardedVideoAd
    self.error = error;
    [self loadReward];
}



@end
